package com.example.j202411.viewmodel;

import android.content.Context;
import android.util.Log;


import com.example.j202411.contract.CatalogContact;
import com.example.j202411.Model.CatalogEntity;
import com.example.j202411.service.CatalogService;

import java.util.Objects;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class CatalogModel implements CatalogContact.CatalogPresenter {

    private CatalogContact.CatalogView mview;
    private Context context;

    public CatalogModel(CatalogContact.CatalogView mview, Context context) {
        this.mview = mview;
        this.context = context;
    }

    @Override
    public void getData(String dtype) {
        String key = "02759a01e0ccbeaae0526556634db600";
        String url = "https://apis.juhe.cn/";
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build();
        CatalogService service = retrofit.create(CatalogService.class);
        Log.d("1234455565", "运行到此处");
        service.getData(dtype, key)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<CatalogEntity>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull CatalogEntity catalogEntity) {
                        if (mview != null) {
                            Log.d("mview111111111111", catalogEntity.toString());
                            mview.showData(catalogEntity);
                        }else {
                            Log.d("空指针异常", "view为null: ");
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.d("51818184518518518184561845", Objects.requireNonNull(e.getMessage()));
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


}
